#!/bin/sh

exec >> /tmp/ansible-rc.local.log 2>&1

# backup
BACKUP=/home/backup-student-from-`date +%Y_%m_%d-%H_%M_%S`
mv /home/student/ $BACKUP
chown root:teacher $BACKUP
chmod 0750 $BACKUP

# create new standart home
rsync -a --chown=student:student /etc/skel/ /home/student/

# add 1580 files
rsync -a --chown=student:student /opt/.lycu1580v2/files/home_student/ /home/student/

# remove mos welcome
rm -f /home/student/.config/autostart/mos-welcome-pages.desktop

# remove kompas 21 if not 1580
[ ! `hostname | grep 1580` ] && rm /home/student/Рабочий\ стол/KOMPAS_3D_21.sh

# remove VistualBox start scripts if no ready
[ ! -f /opt/.lycu1580v2/local/w10k22_vdi/ready04.timestamp ] && rm /home/student/Рабочий\ стол/KOMPAS_3D_22_windows_vm.sh
[ ! -f /opt/.lycu1580v2/local/w10vs2022_vdi/ready04.timestamp ] && rm /home/student/Рабочий\ стол/Visual_Studio_2022_windows_vm.sh
[ ! -f /opt/.lycu1580v2/local/w10pabc_vdi/ready01.timestamp ] && rm /home/student/Рабочий\ стол/PascalABC_windows_vm.sh

# do not automatically start chromium on pannels
[ `hostname | grep p1580` ] && rm /home/student/.config/autostart/chromium.desktop

# remove pannel power managment settings on pannels
#[ `hostname | grep p1580` ] && rm -f /home/student/.config/powermanagementprofilesrc

# set pannel power managment settings in IT-polygon
[ `hostname | grep p1580` ] && rsync -a --chown=student:student /opt/.lycu1580v2/files/opt_1580/powermanagementprofilesrc-p1580 /home/student/.config/powermanagementprofilesrc

[ ! -f /opt/.lycu1580v2/local/student_dot_wine_k21_tflex_pabc_tbz/ready01.timestamp ] && rm /home/student/Рабочий\ стол/KOMPAS_3D_21.sh
[ ! -f /opt/.lycu1580v2/local/student_dot_wine_k21_tflex_pabc_tbz/ready01.timestamp ] && rm /home/student/Рабочий\ стол/T-FLEX_CAD.sh
[ ! -f /opt/.lycu1580v2/local/student_dot_wine_k21_tflex_pabc_tbz/ready01.timestamp ] && rm /home/student/Рабочий\ стол/PascalABC.sh

# remove PascalABC old script if it is absent
[ ! -f /opt/.lycu1580v2/local/student_dot_wine_klt_pabc_tbz/ready01.timestamp ] && rm /home/student/Рабочий\ стол/PascalABC_old.sh

# update student_dot_wine_klt_pabc_current
[ -f /opt/.lycu1580v2/local/student_dot_wine_klt_pabc_tbz/ready01.timestamp ] && rsync -a --delete --chown=student:student /opt/.lycu1580v2/local/student_dot_wine_klt_pabc_tbz/student_dot_wine_klt_pabc/ /home/student_dot_wine_klt_pabc_current/

# delete old (not today) backups
DATE=`date +%Y_%m_%d`
for FILE in /home/backup-*
do
    [ ! "`echo $FILE | grep $DATE`" ] && rm -rf $FILE
done

# disable virtualbox network brige
rmmod vboxnetflt

